package com.mocha.config;

import com.mocha.filter.TestFilterB;
import com.mocha.interceptor.TestInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class TestWebConfig extends WebMvcConfigurerAdapter{


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getTestInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    @Bean
    public TestInterceptor getTestInterceptor(){
        return new TestInterceptor();
    }


    @Bean
    public FilterRegistrationBean myFilterRegistration(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        List<String> urls = new ArrayList<>();
        urls.add("/login");
        registrationBean.setUrlPatterns(urls);
        registrationBean.setFilter(getFilter());
        registrationBean.setName("loginFilter");
        return registrationBean;
    }

    @Bean
    public TestFilterB getFilter(){
        return new TestFilterB();
    }


}
